package com.jhoncy.myproject.util;

import com.jhoncy.myproject.common.exception.ParameterIllegalException;
import org.apache.commons.lang3.StringUtils;

/**
 * 分页工具类
 * Created by zhuangqi on 2017/3/15.
 */
public class PageUtil {
    private static int DEFAIL_START = 1;
    private static int DEFAIL_ROWS = 10;

    public static int parseStart(String start) {
        return parseParameter(start,DEFAIL_START);
    }

    public static int parseRows(String rows) {
        return parseParameter(rows,DEFAIL_ROWS);
    }

    private static int parseParameter(String parameterString, int defaultValue) {
        if (StringUtils.isEmpty(parameterString))
            return defaultValue;
        int parmeter;
        try {
            parmeter = Integer.valueOf(parameterString);
        } catch (NumberFormatException e) {
            throw new ParameterIllegalException();
        }
        if (parmeter < 1)
            throw new ParameterIllegalException();
        return parmeter;
    }
}
